/*
 * Copyright (C) 2018 Logos Technology Ltd.
 */

package com.logos.common.update.dialog;

import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatDialog;

import com.logos.common.R;


/**
 * Created by hk on 2018/4/16.
 */
public class UpdateDialog extends AppCompatDialog {

    private TextView mTvVersion;
    private TextView mTvDesc;
    private TextView mTvLater;
    private TextView mTvUpdate;

    private String mVersion;
    private String mDesc;

    private boolean mIsForce;

    /**
     * dialog的点击事件
     */
    public interface DialogClickListener {

        /**
         * 点击下一步
         */
        void onClickLater();

        /**
         * 点击更新
         */
        void onClickUpdate();

    }

    private DialogClickListener mOnDialogClickListener;

    public void setOnDialogClickListener(DialogClickListener listener) {
        this.mOnDialogClickListener = listener;
    }

    public UpdateDialog(Context context, String version, String desc, boolean isForce) {
        // 在构造方法里, 传入主题
        super(context, R.style.BottomDialogStyle);
        // 拿到Dialog的Window, 修改Window的属性
        Window window = getWindow();
        window.getDecorView().setPadding(0, 0, 0, 0);
        // 获取Window的LayoutParams
        WindowManager.LayoutParams attributes = window.getAttributes();
        attributes.width = WindowManager.LayoutParams.MATCH_PARENT;
        attributes.gravity = Gravity.CENTER_VERTICAL;
        // 一定要重新设置, 才能生效
        window.setAttributes(attributes);
        mVersion = version;
        mDesc = desc;
        mIsForce = isForce;
        setCanceledOnTouchOutside(isForce);
        setCancelable(!isForce);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_update);
        initView();
    }

    private void initView() {
        mTvVersion = findViewById(R.id.tv_version);
        mTvDesc = findViewById(R.id.tv_desc);
        mTvLater = findViewById(R.id.tv_later);
        mTvUpdate = findViewById(R.id.tv_update);

        mTvLater.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mOnDialogClickListener != null) {
                    mOnDialogClickListener.onClickLater();
                }
            }
        });
        mTvUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mOnDialogClickListener != null) {
                    mOnDialogClickListener.onClickUpdate();
                }
            }
        });

        mTvDesc.setText(mDesc);
        mTvVersion.setText(mVersion);

        mTvLater.setVisibility(mIsForce ? View.GONE : View.VISIBLE);
    }
}
